package com.smec.remes.module.query.list.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.RxBus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewListBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.query.Stock;
import com.smec.remes.util.CopyText;
import com.smec.remes.util.ImageDialog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/8.
 */

public class SmecListAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private Context mcontext;
    private ArrayList<Stock> arrayList=new ArrayList<>();
    private static int  NULLDATA = 0;
    private static int  DATA = 1;
    private ImageDialog imageDialog;
    private ArrayList<String> priceList=new ArrayList<>();
    private Bus mRxbus;

    public void _clear(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public SmecListAdapter(Context context,Bus smecBus) {
        this.mcontext=context;
        this.mRxbus=smecBus;
    }

    public void setArrayList(ArrayList<Stock> list,String status,ArrayList<String> priceList){
        arrayList=list;
        for(int i=0;i<list.size();i++){
            arrayList.get(i).setPrice(priceList.get(i));
        }
        notifyDataSetChanged();
    }

    public void setPrice(ArrayList<String> priceList){
        for(int i=0;i<priceList.size();i++){
            arrayList.get(i).setPrice(priceList.get(i));
        }
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA){
            LayoutRecyclerviewListBinding layoutRecyclerviewListBinding=DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_list,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewListBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewListBinding);
            return smecBaseRecyclerViewHolder;
        }else {
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;

        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        if(holder.getViewType() == DATA) {
            final LayoutRecyclerviewListBinding layoutRecyclerviewListBinding= (LayoutRecyclerviewListBinding) holder.getBinding();
            layoutRecyclerviewListBinding.setVariable(BR.stock,arrayList.get(position));
            layoutRecyclerviewListBinding.tvPrice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    layoutRecyclerviewListBinding.tvPrice.setText(arrayList.get(position).getPrice());
                    layoutRecyclerviewListBinding.tvPrice.setTextColor(mcontext.getResources().getColor(R.color.materiel_text));
                }
            });
            layoutRecyclerviewListBinding.llCopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CopyText._copy(arrayList.get(position).getMatnr(),mcontext);
                    Toast.makeText(mcontext,"复制成功",Toast.LENGTH_SHORT).show();
                }
            });
            layoutRecyclerviewListBinding.llBitmapurl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!TextUtils.isEmpty(arrayList.get(position).getDownloadUrl())){
                        imageDialog=new ImageDialog(mcontext,R.style.project_back);
                        ImageView imageView= (ImageView) imageDialog.getCustomView().findViewById(R.id.iv_image);
                        Glide.with(mcontext)
                                .load(arrayList.get(position).getDownloadUrl())
                                .error(R.drawable.smec_glide_error)
                                .into(imageView);
                        imageDialog.show();
                    }
                }
            });
            if(!TextUtils.isEmpty(arrayList.get(position).getDownloadUrl())){
                layoutRecyclerviewListBinding.llPictureAll.setVisibility(View.VISIBLE);
            }else {
                layoutRecyclerviewListBinding.llPictureAll.setVisibility(View.GONE);
            }
        }
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        if(arrayList.size() == 0){
            return 1;
        }else{
            return arrayList.size();
        }

    }

    @Override
    public int getItemViewType(int position) {
        //在这里设置onCreateViewHolde方法内的viewType参数，然后根据这个参数来设置什么样的布局，是占位图还是显示数据的布局
        if(arrayList == null || arrayList.size() == 0){
            return NULLDATA;
        }else{
            return DATA;
        }
    }

}
